package com.sun.rabbit.routing.direct;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.sun.rabbit.utils.RabbitMqUtils;

/**消费者02
 * @author shkstart
 * @create 2021-10-16 21:06
 */
public class ReceiveLogsDirect02 {
    private static final String EXCHANGE_NAME = "direct_logs";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMqUtils.getChannel();
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);
        String QUEUE_NAME="console";
        channel.queueDeclare(QUEUE_NAME,false,false,false,null);
        channel.queueBind(QUEUE_NAME,EXCHANGE_NAME,"info");
        channel.queueBind(QUEUE_NAME,EXCHANGE_NAME,"warning");
        System.out.println("等待接受消息。。。");
        DeliverCallback deliverCallback=(consumerTag, delivery) ->{
            String message = new String(delivery.getBody(), "UTF-8");
            message="接收绑定键:"+delivery.getEnvelope().getRoutingKey()+",消息:"+message;
            System.out.println("消息打印到控制台上："+message);
        };
        channel.basicConsume(QUEUE_NAME,true,deliverCallback,consumerTag -> {System.out.println("消息中断");});

    }
}
